﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Devices;
using System.IO;
using System.Diagnostics;

namespace LyalinSamples.WP7Client.MaVHubInt
{
    public enum MaVAreas {New, History, NowPlaying };

    public class MaVHubIntegrator
    {
        public void CreateImageInMaV(MaVAreas MaVArea)
        {
            // ## Notes ##
            // A. Offical documentation found here: 
            //      - How to: Integrate with the Music and Videos Hub for Windows Phone (http://msdn.microsoft.com/en-us/library/ff769558(v=VS.92).aspx)

            // Create MediaHistoryItem (Common to all integration areas
            MediaHistoryItem MaVItem = new MediaHistoryItem();
            MaVItem.Source = ""; // Not used, leave blank (as of WP7 documentation posted on 12/21/2010)

            // The Title property of the MediaHistoryItem class must be set to text that represents the content, such as a station name or video title. 
            MaVItem.Title = "Sample Title"; // As of 12/21/2010 Title is only displayed in MaV in "Now playing" items (its hidden in the UI for New and History items)

            // The now playing tile must be 358 pixels x 358 pixels in size.
            // Other tiles must be 173 pixels x 173 pixels in size.

            MaVItem.ImageStream = GetImage(MaVArea);
            MaVItem.PlayerContext.Add("Area", MaVArea.ToString());

            // Create item in MaV
            try
            {
                switch (MaVArea)
                {
                    case MaVAreas.History:
                        MediaHistory.Instance.WriteRecentPlay(MaVItem);
                        break;
                
                    case MaVAreas.New:
                        MediaHistory.Instance.WriteAcquiredItem(MaVItem);
                        break;

                    case MaVAreas.NowPlaying:
                        MediaHistory.Instance.NowPlaying = MaVItem;
                        break;
                }

            }
            catch (System.Exception ex)
            {
                // ## Known issue ##
                // If you are in Emulator or Zune is used for debug instead of WPConnect then
                // This will break with a "System.Runtime.InteropServices.COMException (0x803A008A): 0x803a008a" 
                // Workaround: Use the WPConnect.exe tool (Windows Phone Connect Tool: http://msdn.microsoft.com/en-us/library/gg180729(VS.92).aspx)

                Debug.WriteLine(ex.ToString());

                if (Debugger.IsAttached)
                {
                    Debugger.Break();
                }
            }
        }

        private Stream GetImage(MaVAreas MaVArea)
        {
            Stream output = null;

            // ## Important Note - Image Quality
            // Code below has an ImageQuality value that it passes to the generic WriteableBitmapFromUserControl function
            // This has to be different for each MaVArea since the maximum image size is 16384 and if your created
            // Bitmap exceeds it you will get the following error "System.ArgumentException: image stream size bigger than maximum allowed 16384"
            // (I have raised this concern with product group on 10/19/2010 and am waiting for a formal response)

            switch (MaVArea)
            {
                case MaVAreas.History:
                    int ImageQualityHistory = 50; // Pick value that wont exceed 16384 (see above note)
                    output = UCToBitmapHelper.WriteableBitmapFromUserControl(
                                                new HistoryIcon(), 358, 358, ImageQualityHistory);
                    break;
                
                case MaVAreas.New:
                    int ImageQualityNew = 50; // Pick value that wont exceed 16384 (see above note)
                    output = UCToBitmapHelper.WriteableBitmapFromUserControl(
                                     new NewItemIcon(), 358, 358, ImageQualityNew);
                    break;
                
                case MaVAreas.NowPlaying:
                    int ImageQualityNowPlaying = 50; // Pick value that wont exceed 16384 (see above note)
                    output = UCToBitmapHelper.WriteableBitmapFromUserControl(
                            new NowPlayingIcon(), 358, 358, ImageQualityNowPlaying);
                    break;
            }

            return output;
        }
    }
}
